<!DOCTYPE html>
<html lang="en">
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta charset="utf-8" />
		<title>Blank Page - Ace Admin</title>

		<meta name="description" content="" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

		<!-- bootstrap & fontawesome -->
		<link rel="stylesheet" href="#(CPATH)/assets/css/bootstrap.css" />
		<link rel="stylesheet" href="#(CPATH)/assets/css/font-awesome.css" />

		<!-- page specific plugin styles -->
		<link rel="stylesheet" href="#(CPATH)/assets/css/bootstrap-table.css">
		<link rel="stylesheet" href="#(CPATH)/assets/css/basejace.css">
		<link rel="stylesheet" href="#(CPATH)/assets/js/jstree/themes/default/style.css"><!-- 使用jstree插件 -->
		
		<!-- text fonts -->
		<link rel="stylesheet" href="#(CPATH)/assets/css/ace-fonts.css" />

		<!-- ace styles -->
		<link rel="stylesheet" href="#(CPATH)/assets/css/ace.css" class="ace-main-stylesheet" id="main-ace-style" />

		<!--[if lte IE 9]>
			<link rel="stylesheet" href="#(CPATH)/assets/css/ace-part2.css" class="ace-main-stylesheet" />
		<![endif]-->

		<!--[if lte IE 9]>
		  <link rel="stylesheet" href="#(CPATH)/assets/css/ace-ie.css" />
		<![endif]-->

		<!-- inline styles related to this page -->

		<!-- ace settings handler -->
		<script src="#(CPATH)/assets/js/ace-extra.js"></script>

		<!-- HTML5shiv and Respond.js for IE8 to support HTML5 elements and media queries -->

		<!--[if lte IE 8]>
		<script src="#(CPATH)/assets/js/html5shiv.js"></script>
		<script src="#(CPATH)/assets/js/respond.js"></script>
		<![endif]-->
	</head>

	<body class="no-skin">
		<div class="main-container" id="main-container">
			<script type="text/javascript">
				try{ace.settings.check('main-container' , 'fixed')}catch(e){}
			</script>
			<div class="main-content">
				<div class="page-content">
					<!-- 提示信息条 -->
					#if(message != null)
					<div class="row">
						<div class="col-xs-12">
							<div class="alert alert-info">
								<button type="button" class="close" data-dismiss="alert">
									<i class="ace-icon fa fa-times"></i>
								</button>
								#(message)
							</div>
						</div>
					</div><!-- ./提示信息条 -->
					#end
					<div class="row">
						<div class="col-xs-12">
							<div id="controlTree" style="padding-left:2px;">
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-xs-12 text-center">
							<button class="btn btn-sm btn-info" onclick="doSave()">保存</button>
						</div>
					</div>
				</div> <!-- ./page-content -->
			</div> <!-- ./main-content -->
		</div>
		<!-- basic scripts -->
		<!--[if !IE]> -->
		<script type="text/javascript">
			window.jQuery || document.write("<script src='#(CPATH)/assets/js/jquery.js'>"+"<"+"/script>");
		</script>

		<!-- <![endif]-->

		<!--[if IE]>
<script type="text/javascript">
 window.jQuery || document.write("<script src='#(CPATH)/assets/js/jquery1x.js'>"+"<"+"/script>");
</script>
<![endif]-->
		<script type="text/javascript">
			if('ontouchstart' in document.documentElement) document.write("<script src='#(CPATH)/assets/js/jquery.mobile.custom.js'>"+"<"+"/script>");
		</script>
		<script src="#(CPATH)/assets/js/bootstrap.js"></script>

		<!-- page specific plugin scripts -->
		<script src="#(CPATH)/assets/js/bootstrap-table/bootstrap-table.js"></script>
		<script src="#(CPATH)/assets/js/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
		<script src="#(CPATH)/assets/js/bootbox.js"></script>
		<script src="#(CPATH)/assets/js/jstree/jstree.js"></script>
		
		<!-- ace scripts -->
		<script src="#(CPATH)/assets/js/ace/elements.scroller.js"></script>
		<script src="#(CPATH)/assets/js/ace/elements.colorpicker.js"></script>
		<script src="#(CPATH)/assets/js/ace/elements.fileinput.js"></script>
		<script src="#(CPATH)/assets/js/ace/elements.typeahead.js"></script>
		<script src="#(CPATH)/assets/js/ace/elements.wysiwyg.js"></script>
		<script src="#(CPATH)/assets/js/ace/elements.spinner.js"></script>
		<script src="#(CPATH)/assets/js/ace/elements.treeview.js"></script>
		<script src="#(CPATH)/assets/js/ace/elements.wizard.js"></script>
		<script src="#(CPATH)/assets/js/ace/elements.aside.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.ajax-content.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.touch-drag.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.sidebar.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.sidebar-scroll-1.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.submenu-hover.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.widget-box.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.settings.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.settings-rtl.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.settings-skin.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.widget-on-reload.js"></script>
		<script src="#(CPATH)/assets/js/ace/ace.searchbox-autocomplete.js"></script>

		<!-- inline scripts related to this page -->
		<script type="text/javascript">
			var cmdView = "#(CPATH)/system/control/view";
			var cmdDelete = "#(CPATH)/system/user/delete";
			$(function(){
				var hasload = false;
				$("#controlTree")
					.bind("changed.jstree", function (e, data) {
						if(data.selected.length) {
							//alert('The selected node is: ' + data.instance.get_node(data.selected[0]).text);
							console.log("select the node: " + data.instance.get_node(data.selected[0]).id );
						}
					})
					.bind("loaded.jstree", function (e, data) {
						data.instance.open_all();
						
						//$(this).jstree("open_all"); 
						var d ;
						//data.instance.select_node('000102');
						$.ajax({
							url: "#(CPATH)/system/authority/getContorlids?objectid=" + #(roleid) + "&objecttype=1",
							async: "true", //这里设置同步方式
							dataType: "json",
							success: function(d){
						        if (typeof d[0] === "string") {
						        	//console.log(d[0]);
						        	//alert(data);
						        }
						        if (typeof d === "object") {
						        	//alert("object:" + data[2]); 
						        }
						        /* for(var i=0;i<d.length;i++){
						        	data.instance.select_node(d[i]);
						        } */
						        data.instance.select_node(d);
						       
							}
						});
						
					})
					.jstree({
						'plugins':["wholerow","checkbox"],
						'core' : {
							'multiple' : true,
							'data'     : {
								'url'  : "#(CPATH)/system/control/tree",
								'dataType' : "json",
								'async': "false",
								'success': function(data){
									//alert(data);
									//select_node("0001");
									hasload = true;
								}
							}
						}
					});
				
				//设置父弹出框的高度
				if(parent){
					var pTar = parent.document.getElementById("authoritytree");
					pTar.height = 350;
				}
				
				
			});
			
			
			function doSave() {
				var instance = $("#controlTree").jstree(true);
				
				var nodes = instance.get_checked(true);
				var a = "";
				$.each(nodes, function(i, node) {
					//a += node.id + ",";
					//var node = ids[i];
			        each(node, instance, nodes);

				});
				
				$.each(nodes, function(i, node) {
					a+=node.id + ",";
				});
					//alert(a);
				
				location.href = "#(CPATH)/system/authority/reset?roleid=" + #(roleid) + "&controlids=" + a;
			}
			
			//递归获取选中节点的所有父节点 放到 nodes数组中
			function each(node, tree, nodes) {
				var pid = tree.get_parent(node);
				var p = tree.get_node(pid);
				if (p) {
					if (nodes&&p&&nodes.indexOf(p)<0)
						nodes.push(p);
					each(p, tree, nodes);
				}
			}
		</script>
	</body>
</html>
